package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.List;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.InpChargeIndicator;
import com.th.supcom.pds.impl.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.impl.common.enums.InpWithDrawType;
import com.th.supcom.pds.medicare.api.AdapterBMSChargeService;

/**
 * 药房退药时退费命令
 * 
 * @author yzhang
 */
public class PhamReturnCharge extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext phamDispenseContext = (PhamDispenseContext) arg0;
        List <InpPhamApplyDetailVO> inpPhamApplyDetailVOs = phamDispenseContext.getApplyDetailVOList ();
        List <InpPhamDispenseDetailVO> inpPhamDispenseDetailVOs = phamDispenseContext.getInpPhamDispenseDetailVOList ();
        String withDrawType = phamDispenseContext.getWithdrawType ();
        // TODO MOCK调用BMS接口实现
        // 更新费用单据号,更改状态
        AdapterBMSChargeService adapterBMSChargeService = getAdapterBMSChargeService ();
        adapterBMSChargeService.inpPhamRefund (inpPhamDispenseDetailVOs);
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : inpPhamDispenseDetailVOs)
        {
            if (InpWithDrawType.ReturnWithDrug.getEnumCode ().equals (withDrawType))
            {
                inpPhamDispenseDetailVO.setDispenseIndicator (InpDispenseIndicator.HasDrugReturn.getEnumCode ());
            }
            else
            {
                inpPhamDispenseDetailVO.setDispenseIndicator (InpDispenseIndicator.OnlyRefunded.getEnumCode ());
            }
            inpPhamDispenseDetailVO.setChargeIndicator (InpChargeIndicator.Refunded.getEnumCode ());
        }

        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : inpPhamApplyDetailVOs)
        {
            if (InpWithDrawType.ReturnWithDrug.getEnumCode ().equals (withDrawType))
            {
                inpPhamApplyDetailVO.setDispenseIndicator (InpDispenseIndicator.HasDrugReturn.getEnumCode ());
            }
            else
            {
                inpPhamApplyDetailVO.setDispenseIndicator (InpDispenseIndicator.OnlyRefunded.getEnumCode ());
            }
            inpPhamApplyDetailVO.setChargeIndicator (InpChargeIndicator.Refunded.getEnumCode ());
        }
        phamDispenseContext.setInpPhamDispenseDetailVOList (inpPhamDispenseDetailVOs);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamDispenseCharge.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
